<?php

require_once __DIR__ . "/functions.php";

class Helper
{

    private $_em;
    private $_dataBilling;

    function __construct($em)
    {
        $this->_em = $em;

        if ($this->isFiltering()) {
            $this->_dataBilling = $this->_getMapBillingFilter($_GET['dateBegin'], $_GET['dateEnd'], $_GET['type']);
        } else {
            $this->_dataBilling = $this->_getMapBillingDefault();
        }
    }
    
    public function getDataForSim($idSim)
    {
        if (isset($this->_dataBilling[$idSim])) {
            return $this->_dataBilling[$idSim];
        }
        return null;
    }

    public function getDataBilling()
    {
        return $this->_dataBilling;
    }

    public function isFiltering()
    {
        return isset($_GET['dateBegin']) && isset($_GET['dateEnd']);
    }

    private function _getMapBillingFilter($dateBegin, $dateEnd, $type)
    {
        $result = array();

        $dateBegin = convertDate($dateBegin);
        $dateEnd = convertDate($dateEnd);

        $where = "where date between '$dateBegin 00:00:00' and '$dateEnd 23:59:59' and type = '$type'";
        $sql = "select phone, paysystem, max(date) as sss, max(date) as maxdate from biling $where group by phone";

        $rows = $this->_em->getConnection()->query($sql)->fetchAll();

        foreach ($rows as $row) {
            //print_r($row);
            $result[$row['phone']] = array(
                'maxdate' => $row['maxdate'],
                'maxdateFillup' => null,
                'paysystem' => $row['paysystem']
            );
        }
//exit();
        return $result;
    }
    /**
     * 
     * @param array ('id sim'=>array(
     *      'maxdate'=>'2013-05-05'
     *      'maxdateFillup'=>'2013-05-05'
     *      'paysystem'=>'Chronopay'
     * ))
     * @return null
     */
    private function _getMapBillingDefault()
    {        
        $dataOrigTerm = $this->_getMapBillingForType(array('ORIG', 'TERM'));
        
        
        $dataFillup = $this->_getMapBillingForType(array('FILLUP'));
        
        // собираем данные
        foreach ($dataOrigTerm as $idSim => $data) {
            if (isset($dataFillup[$idSim])) {
                $data['maxdateFillup'] = $dataFillup[$idSim]['maxdate'];                
                unset($dataFillup[$idSim]);
            } else {
                $data['maxdateFillup'] = null;
            }
            $dataOrigTerm[$idSim] = $data;
        }
        
        
        
        foreach ($dataFillup as $idSim => $data) {
            $data['maxdateFillup'] = $data['maxdate'];
            $data['maxdate'] = null;
            $dataOrigTerm[$idSim] = $data;
        }
       
        
        return $dataOrigTerm;
        
    }
    
    private function _getMapBillingForType(array $types)
    {
        $types = sprintf("'%s'", implode("','", $types));
        
        $where = " where id in ( select max(id) from biling group by phone ) 
                    and type in ( $types )";
        $sql = " select phone, date as maxdate, paysystem from biling $where ";

        $rows = $this->_em->getConnection()->query($sql)->fetchAll();

        foreach ($rows as $row) {
            $result[$row['phone']] = $row;
        }

        return $result;
    }
    
    public static function dateFormat($stringDate){
        $date = DateTime::createFromFormat('Y-m-d H:i:s', $stringDate);
        return $date->format('d.m.Y');
    }

}
